package com.ryit.im.controller;

import com.alibaba.fastjson.JSONObject;
import com.ryit.im.base.AjaxResult;
import com.ryit.im.base.PageParam;
import com.ryit.im.model.dto.group.GroupApplyDto;
import com.ryit.im.model.dto.group.GroupChatDto;
import com.ryit.im.model.dto.group.GroupDto;
import com.ryit.im.model.dto.user.GroupUserInfoDto;
import com.ryit.im.model.pojo.group.GroupApply;
import com.ryit.im.pojo.base.Result;
import com.ryit.im.pojo.dto.ImGroupDto;
import com.ryit.im.service.ImGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: lsx
 * @description:
 * @date: 2020/4/7
 */
@RestController
@RequestMapping("/group")
public class GroupController {
    @Autowired
    private ImGroupService imGroupService;

    @GetMapping(value = "/list")
    public AjaxResult getGroupList(ImGroupDto groupDto){
        return imGroupService.queryGroups(groupDto);
    }

    /**
     * 查询群组详情
     * @param id
     * @param token
     * @return
     */
    @GetMapping(value = "/detail")
    public AjaxResult queryGroupDetail(String id,String token){
        return imGroupService.queryGroupDetail(id,token);
    }

    /**
     * 查询群组成员
     * @param id
     * @param token
     * @return
     */
    @GetMapping(value = "/users")
    public AjaxResult queryGroupUsers(String id,String token){
        return imGroupService.queryGroupUsers(id,token);
    }

    /**
     * 转让群
     * @param dto
     * @param token
     * @return
     */
    @PostMapping(value = "/abandon")
    public AjaxResult abandon(@RequestBody GroupDto dto, String token){
        return imGroupService.groupAbandon(dto);
    }

    /**
     * 设置管理员
     * @param dto
     * @param token
     * @return
     */
    @PostMapping(value = "/setAdmin")
    public AjaxResult setAdmin(@RequestBody GroupUserInfoDto dto, String token ){
        return imGroupService.groupSetAdmin(dto);
    }

    /**
     * 创建群聊
     * @param dto
     * @return
     */
    @PostMapping(value = "/create")
    public AjaxResult create(@RequestBody GroupDto dto ){
        return imGroupService.createGroup(dto);
    }

    /**
     * 修改群聊
     * @param dto
     * @return
     */
    @PutMapping(value = "/update")
    public AjaxResult update(@RequestBody GroupDto dto){
        return imGroupService.updateGroup(dto);
    }

    /**
     * 查询群聊记录
     * @param dto
     * @param token
     * @return
     */
    @GetMapping(value = "/record")
    public AjaxResult queryGroupRecord(GroupChatDto dto,PageParam pageParam ,String token){
        return imGroupService.queryGroupRecord(pageParam,dto,token);
    }

    /**
     * 邀请入群
     * @param dto
     * @return
     */
    @PostMapping(value = "/invite")
    public AjaxResult inviteToGroup(GroupApply dto){
        return imGroupService.inviteToGroup(dto);
    }

    /**
     *入群审核
     * @param dto
     * @return
     */
    @PutMapping(value = "/examine")
    public AjaxResult examineToGroup(GroupApplyDto dto){
        return imGroupService.examineToGroup(dto);
    }

    /**
     * 群二维码
     * @param response
     * @param id
     * @throws IOException
     */
    @GetMapping(value = "/code")
    public void groupQrCode(HttpServletResponse response, String id) throws IOException {
        imGroupService.getCode(response ,id);
    }

    /**
     * 会话列表
     * @param token
     * @return
     */
    @GetMapping(value = "/getSessions")
    public AjaxResult getSessions(String token) {
        return imGroupService.getSessions(token);
    }

    /**
     * 会话置顶
     * @param id
     * @param status
     * @param token
     * @return
     */
    @GetMapping(value = "/setSessionTop")
    public AjaxResult setSessionTop(String id,Integer status,String token) {
        return imGroupService.setSessionTop(id,status,token);
    }

    /**
     * 删除会话
     * @param id
     * @param token
     * @return
     */
    @DeleteMapping(value = "/delete")
    public AjaxResult setSessionTop(String id,String token) {
        return imGroupService.deleteSessionTop(id,token);
    }

    /**
     * 进入会话
     * @param fid
     * @param token
     * @return
     */
    @GetMapping(value = "/intoSession")
    public Result intoSession(PageParam pageParam, String fid, String token) {
        return imGroupService.querySession(pageParam,fid,token);
    }

    @PutMapping("setRemark/{gid}/{name}/{token}")
    public Result setRemark(@PathVariable String gid,@PathVariable String name,@PathVariable String token){
        return imGroupService.setUserRemarks(gid,name,token);
    }
    @PutMapping(value ="groupRemarks/{gid}/{name}/{token}")
    public Result setGroupRemarks(@PathVariable String gid,@PathVariable String name,@PathVariable String token){
        return imGroupService.setGroupRemarks(gid,name,token);
    }

    @PutMapping(value ="removeGroup")
    public Result setGroupRemarks(@RequestBody JSONObject jsonObject){
        return imGroupService.removeGroup(jsonObject.getString("gid"),jsonObject.getString("uid"),jsonObject.getString("token"));
    }



}

